package pearson.automation.forms;

import org.openqa.selenium.By;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.Label;

public class LightBoxForm extends BaseForm {
    private static Label lightBox = new Label(By.id("mbCenter"), "Light Box");
    private Label title = new Label(By.id("mbTitle"), "Title");
    private Button close = new Button(By.id("mbCloseLink"), "Close");
    
    private Label iframe = new Label(By.tagName("iframe"), "Iframe");
    private Label loading = new Label(By.id("loading"), "Loading");
    private Label errorMessage = new Label(By.className("error-message"), "Error Message");
    
    public LightBoxForm() {
        super(lightBox.getLocator(), "Light box");
        //Wait for loading class to dissappear
        lightBox.waitForElementToBeLoaded();
    }

    public boolean isPresent() {
        return lightBox.getElement().getCssValue("display").equals("block");
    }
    
    public void waitForPreviewToBeLoaded() {
        loading.waitForElementDissapear();
    }
    
    public boolean hasErrorMessage() {
        boolean result = true;
        if (iframe.isPresent()) {
            browser.getDriver().switchTo().frame(0);
            result = errorMessage.isPresent();
            browser.getDriver().switchTo().defaultContent();
        } else {
            result = errorMessage.isPresent();
        }
        return result;
    }
    
    public String getTitle() {
        return title.getText();
    }
    
    public void clickClose() {
        close.clickAndWait();
    }
    
}
